package org.nlp2rdf.navigator.client;

import java.util.List;

import org.nlp2rdf.navigator.client.common.SearchDescriptorDTO;
import org.nlp2rdf.navigator.client.common.SearchResultDTO;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import org.nlp2rdf.navigator.client.functionality.Tooltip;

public class SearchWidget extends Composite{
	
	WorkerServiceAsync worker = WorkerServiceAsync.Util.getInstance();
	final Application app;
	
	public SearchWidget(final Application app, List<SearchDescriptorDTO> availableSearches) {
		this.app = app;
		
		VerticalPanel mainPanel = getMainPanel();
	
		for (SearchDescriptorDTO search : availableSearches) {
			if(search.getSearchType().equalsIgnoreCase("text")){
				mainPanel.add(getTextSearchPanel( search));
			}
		}
		mainPanel.addStyleName("greyed");
		initWidget(mainPanel);
	}
		

		
	
	private VerticalPanel getMainPanel(){
		VerticalPanel mainPanel = new VerticalPanel();
		
		mainPanel.addStyleName("module-border");
		mainPanel.setWidth("95%");
		
		Label moduleTitle = new Label("Search");
		moduleTitle.addStyleName("module-title");
		mainPanel.add(moduleTitle);
		return mainPanel;
	}

	private Panel getTextSearchPanel(final SearchDescriptorDTO search){
		final Label label = new Label(search.getLabel());
		final TextBox input = new TextBox();


		final Button submit = new Button("search");
		Tooltip.assignToolTip(submit, new Label(search.getComment()));
        submit.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent submitClick) {
				String searchText = input.getText().trim();
				if(searchText.length()<=1 ){
					Window.alert("must be longer than 1 char: \""+searchText+"\"");
					return;
				}
				
				submit.setEnabled(false);
				submit.setText("searching...");
				worker.search(search.getSearchId(),input.getText(), 50, new AsyncCallback<SearchResultDTO>() {
					public void onSuccess(SearchResultDTO searchResult) {
						resetButton();
						app.displaySearchResult( searchResult);
					}
					public void onFailure(Throwable throwable) {
						resetButton();
						app.reportError(throwable);
					}
					
					private void resetButton(){
						submit.setEnabled(true);
						submit.setText("search");
					}
					
				});
			}
		});
		
		input.addKeyUpHandler(new KeyUpHandler() {
			
			public void onKeyUp(KeyUpEvent arg0) {
				if(arg0.getNativeKeyCode()==13){
					submit.click();
				}
			}
		});
		

	
		HorizontalPanel result = new HorizontalPanel();
		result.setWidth("100%");

       
        result.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		result.add(label);
        result.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
		result.add(input);
		result.add(submit);
		
		result.setCellHorizontalAlignment(label, HasHorizontalAlignment.ALIGN_LEFT);
		result.setCellVerticalAlignment(label, HasVerticalAlignment.ALIGN_MIDDLE);
		result.setCellHorizontalAlignment(input, HasHorizontalAlignment.ALIGN_RIGHT);
		result.setCellHorizontalAlignment(submit, HasHorizontalAlignment.ALIGN_RIGHT);
		
		return result;
	}
}
	
	
	
	


